import org.junit.Test;

import java.time.*;
import java.time.format.DateTimeFormatter;

public class JavaTimeTest {
    public static void main(String[] args) {

    }

    @Test
    public void test1() {
        //获取当前时间
        LocalDate ld = LocalDate.now();
        System.out.println(ld);
        //设置制定的年月日时间
        LocalDate ldSet = LocalDate.of(2024, 3, 2);
        System.out.println(ldSet);

        //字符串时间转为LocalDate对象
        LocalDate ldParse = LocalDate.parse("2024-09-02");
        System.out.println(ldParse);

        //获取方法
        int Year = ldParse.getYear();
        int Month = ldParse.getMonthValue();
        int Day1 = ldParse.getDayOfWeek().getValue();
        int Day2 = ldParse.getDayOfMonth();
        int Day3 = ldParse.getDayOfYear();
        System.out.println(Year + " " + Month + " " + Day1 + " " + Day2 + " " + Day3);
        boolean isLeapYear = ldParse.isLeapYear();
        System.out.println("是否闰年：" + isLeapYear);
        int days1 = ld.lengthOfMonth();
        int days2 = ld.lengthOfYear();
        System.out.println("本月：" + days1 + "天，本年" + days2 + "天");



        //加减方法
        LocalDate tomorrow = ld.plusDays(1);
        System.out.println("明天是："+tomorrow);

        LocalDate nextWeek = ld.plusWeeks(1);
        System.out.println("下周是："+nextWeek);

        LocalDate nextMonth = ld.plusMonths(1);
        System.out.println("下个月是："+nextMonth);

        LocalDate nextYear = ld.plusYears(1);
        System.out.println("明年是："+nextYear);

        LocalDate yesterday = ld.minusDays(1);
        System.out.println("昨天是："+yesterday);

        LocalDate lastWeek = ld.minusWeeks(1);
        System.out.println("上周是："+lastWeek);

        LocalDate lastMonth = ld.minusMonths(1);
        System.out.println("上个月是："+lastMonth);

        LocalDate lastYear = ld.minusYears(1);
        System.out.println("去年今天是："+lastYear);

        //计算跨度
        Period pd = Period.between(LocalDate.of(2024,1,1), LocalDate.of(2024,1,10));
        int d11 = pd.getDays();
        int m1 = pd.getMonths();
        int y1 = pd.getYears();
        System.out.println("时间间隔是：" + d11 + "天或" + m1 + "月或" + y1 + "年");

        //比较方法
        LocalDate l1 = LocalDate.of(2025, 1, 1);
        LocalDate l2 = LocalDate.of(2025, 1, 3);

        System.out.println(l1.isBefore(l2));
        System.out.println(l1.isAfter(l2));
        System.out.println(l1.isEqual(l2));

        DateTimeFormatter d1 = DateTimeFormatter.ofPattern("yyyy/MM/dd");
        System.out.println(ld.format(d1));

        String str = "2020/08/09";
        //字符串必须和格式一致才可解析
        LocalDate parseStr = LocalDate.parse(str, d1);
        System.out.println(parseStr);
    }

    @Test
    public void test2() {
        LocalTime lt = LocalTime.now();
        System.out.println(lt);
        LocalTime ltSet = LocalTime.of(10,1,10, 13400000);
        System.out.println(ltSet);

        //字符串转换为LocalTime对象
        LocalTime ltParse = LocalTime.parse("10:01:01.12300");
        System.out.println(ltParse);

        //get方法
        int hour = lt.getHour();
        int minutes = lt.getMinute();
        int seconds = lt.getSecond();
        int nano = lt.getNano();

        System.out.println("hour is " + hour + ", minutes is " + minutes + ", seconds is "+
                seconds + ", nano is " + nano);

        //操作方法
        LocalTime nextHour = lt.plusHours(1);
        System.out.println("下个小时时间是：" + nextHour);

        LocalTime nextMinute = lt.plusMinutes(1);
        System.out.println("下一分钟时间是：" + nextMinute);

        LocalTime nextSecond = lt.plusSeconds(10);
        System.out.println("十秒后时间是：" + nextSecond);

        LocalTime lastHour = lt.minusHours(1);
        System.out.println("上一个小时时间是：" + lastHour);

        //LocalTime转为格式化的字符串
        DateTimeFormatter dt = DateTimeFormatter.ofPattern("HH:mm:ss");
        String dtTime = lt.format(dt);
        System.out.println(dtTime);

        //获取最大最小时间
        LocalTime MinTime = LocalTime.MIN;
        LocalTime MaxTime = LocalTime.MAX;
        System.out.println("今天最小时间：" + MinTime +", 今天最大时间：" + MaxTime);

        //比较时间大小
        Duration duration = Duration.between(LocalTime.of(16,0,0), LocalTime.of(17,0,0));
        long hours1 = duration.toHours();
        long minutes1 = duration.toMinutes();
        System.out.println("跨度为" + hours1 + "小时或者" + minutes1 + "分钟");

        //配合使用
        LocalDate date1 = LocalDate.of(2024,1,1);
        LocalDateTime ldt = date1.atTime(lt);
        System.out.println(ldt);

        LocalTime lt1 = ldt.toLocalTime();
        System.out.println(lt1);

        //配合时区
        ZonedDateTime zonedDateTime = ZonedDateTime.now(ZoneId.of("Europe/Paris"));
        LocalTime elt = zonedDateTime.toLocalTime();
        System.out.println("巴黎时间：" + elt);
    }


    @Test
    public void test3() {
        LocalDateTime ldt = LocalDateTime.now();
        System.out.println(ldt);
        LocalDateTime ldt1 = LocalDateTime.of(2024,1,1,22,10,12);
        System.out.println(ldt1);

        LocalDateTime ldtParse = LocalDateTime.parse("2024-01-01T14:00:00");
        System.out.println(ldtParse);

        //get方法
        int hour2 = ldt.getHour();
        int minutes2 = ldt.getMinute();
        int seconds2 = ldt.getSecond();
        int year2 = ldt.getYear();
        int month2 = ldt.getMonthValue();
        int day2 = ldt.getDayOfMonth();
        boolean isLeap2 = ldt.toLocalDate().isLeapYear();
        int days3 = ldt.toLocalDate().lengthOfMonth();
        int day4  = ldt.toLocalDate().lengthOfYear();

        System.out.println(year2 +"-" +month2+ "-" + day2 +" " + hour2 + ":" + minutes2 + ":" + seconds2);

        LocalDateTime tomorrow = ldt.plusDays(1);
        LocalDateTime yesterday = ldt.minusDays(1);;


        LocalDateTime ldt2 = LocalDateTime.of(2024,1,1,22,10,12);
        LocalDateTime ldt3 = LocalDateTime.of(2024,1,2,22,10,12);
        System.out.println(ldt2.isBefore(ldt3));
        System.out.println(ldt2.isAfter(ldt3));
        System.out.println(ldt2.isEqual(ldt3));

        DateTimeFormatter d2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        System.out.println(ldt2.format(d2));

        String str2 = "2024-02-01T14:00:00";
        LocalDateTime ldt4 = LocalDateTime.parse(str2);
        //使用制定格式转换需要parse接受一个格式参数才行
        String str3 = "2024-02-01 14:00:00";
        LocalDateTime ldt5 = LocalDateTime.parse(str3,d2);
        System.out.println(ldt4);
        System.out.println(ldt5);
    }
}
